import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.AndTerm;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SentDateTerm;
import javax.mail.search.SubjectTerm;


public class ForwardMail
{
    private String from;

    private String to;
    
    private Message messages[];
    
    private final static String pwd = "lovegq";
    
    private final static String CONFIRM_SUBJECT = "你的預訂申請已被確認";
    
    private Folder folder;
    
    private Store store;
    
    public static void main(String[] args)
    {
//        ForwardMail mail = new ForwardMail("junhe3312@163.com", "junhe3311@163.com");
//        ForwardMail mail = new ForwardMail("junhe3312@163.com", "junhehk01-01@yahoo.cn");
        ForwardMail mail = new ForwardMail("junhehk01-01@yahoo.cn", "junhe3312@163.com");
        mail.connect();
        mail.check();
    }
    
    public ForwardMail(String from, String to)
    {
        this.from = from;
        this.to = to;
    }
    
    public void connect()
    {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props);
        try
        {
            store = session.getStore("pop3");
//            store.connect("pop3.163.com", from, pwd);
            store.connect("pop3.yahoo.cn", "junhehk@yahoo.cn", pwd);
            folder = store.getFolder("Inbox");
            folder.open(Folder.READ_ONLY);
            
            Calendar c = Calendar.getInstance();
            SentDateTerm endDateTerm = new SentDateTerm(ComparisonTerm.GE, c.getTime());
            c.add(Calendar.HOUR, -24);
            SentDateTerm startDateTerm = new SentDateTerm(ComparisonTerm.LT, c.getTime());
            SearchTerm dateTerm = new AndTerm(startDateTerm, endDateTerm);
            
            SearchTerm subjectTerm = new SubjectTerm(CONFIRM_SUBJECT);
            messages = folder.search(new AndTerm(dateTerm, subjectTerm));
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    
    public void check()
    {
        if (messages == null)
        {
            return;
        }
        for (int i = 0; i < messages.length; i++)
        {
            Message mes = messages[i];
            try
            {
                System.out.println(mes.getReplyTo());
                Address[] address = mes.getRecipients(Message.RecipientType.TO);
                for (int j = 0; j < address.length; j++)
                {
                    System.out.println(address[j]);
                }
                forward(mes);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
    
    private void forward(Message mes) throws Exception
    {
        Properties prop = new Properties();
        prop.setProperty("mail.transport.protocol", "smtp");  
        prop.setProperty("mail.smtp.auth", "true");  
        prop.setProperty("mail.host", "smtp.163.com");  
        Session session = Session.getInstance(prop, new Authenticator()
        {
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(from, pwd);
            }
        });
        session.setDebug(false);
        
        MimeMessage forward = new MimeMessage(session);
        forward.setDataHandler(mes.getDataHandler());
        forward.setSubject("Fwd:" + mes.getSubject());
        forward.setFrom(new InternetAddress(from));
        
        InternetAddress[] address = { new InternetAddress(to) };
        forward.setRecipients(Message.RecipientType.TO, address);
        forward.setSentDate(new Date());
        
        Multipart mp = new MimeMultipart();
        BodyPart bp = new MimeBodyPart();
        bp.setContent(mes.getContent(), "text/html;charset=GB2312");
        mp.addBodyPart(bp);
        forward.setContent(mp);
        
        Transport.send(forward);
        folder.close(false);
        store.close();
    }
}
